<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="user id" prop="uid">
      <el-input v-model="dataForm.uid" placeholder="user id"></el-input>
    </el-form-item>
    <el-form-item label="分区 id" prop="pid">
      <el-input v-model="dataForm.pid" placeholder="分区 id"></el-input>
    </el-form-item>
    <el-form-item label="保留为以后的视频分级别，0正常，" prop="level">
      <el-input v-model="dataForm.level" placeholder="保留为以后的视频分级别，0正常，"></el-input>
    </el-form-item>
    <el-form-item label="-1代表审核未通过, 0代表审核中，1代表审核通过" prop="status">
      <el-input v-model="dataForm.status" placeholder="-1代表审核未通过, 0代表审核中，1代表审核通过"></el-input>
    </el-form-item>
    <el-form-item label=" 视频链接" prop="url">
      <el-input v-model="dataForm.url" placeholder=" 视频链接"></el-input>
    </el-form-item>
    <el-form-item label=" 封面" prop="cover">
      <el-input v-model="dataForm.cover" placeholder=" 封面"></el-input>
    </el-form-item>
    <el-form-item label="视频时长" prop="videoTime">
      <el-input v-model="dataForm.videoTime" placeholder="视频时长"></el-input>
    </el-form-item>
    <el-form-item label=" 视频集数，如果需要上传多集，会用到" prop="seriesNum">
      <el-input v-model="dataForm.seriesNum" placeholder=" 视频集数，如果需要上传多集，会用到"></el-input>
    </el-form-item>
    <el-form-item label=" 标题" prop="title">
      <el-input v-model="dataForm.title" placeholder=" 标题"></el-input>
    </el-form-item>
    <el-form-item label=" 0转载or1自制,默认自制" prop="videoType">
      <el-input v-model="dataForm.videoType" placeholder=" 0转载or1自制,默认自制"></el-input>
    </el-form-item>
    <el-form-item label=" 转载权限0不可转（默认）1可转" prop="rePrintPower">
      <el-input v-model="dataForm.rePrintPower" placeholder=" 转载权限0不可转（默认）1可转"></el-input>
    </el-form-item>
    <el-form-item label=" 转载自" prop="fromUrl">
      <el-input v-model="dataForm.fromUrl" placeholder=" 转载自"></el-input>
    </el-form-item>
    <el-form-item label=" 简介" prop="remark">
      <el-input v-model="dataForm.remark" placeholder=" 简介"></el-input>
    </el-form-item>
    <el-form-item label="" prop="uploadTime">
      <el-input v-model="dataForm.uploadTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          uid: '',
          pid: '',
          level: '',
          status: '',
          url: '',
          cover: '',
          videoTime: '',
          seriesNum: '',
          title: '',
          videoType: '',
          rePrintPower: '',
          fromUrl: '',
          remark: '',
          uploadTime: '',
          updateTime: ''
        },
        dataRule: {
          uid: [
            { required: true, message: 'user id不能为空', trigger: 'blur' }
          ],
          pid: [
            { required: true, message: '分区 id不能为空', trigger: 'blur' }
          ],
          level: [
            { required: true, message: '保留为以后的视频分级别，0正常，不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '-1代表审核未通过, 0代表审核中，1代表审核通过不能为空', trigger: 'blur' }
          ],
          url: [
            { required: true, message: ' 视频链接不能为空', trigger: 'blur' }
          ],
          cover: [
            { required: true, message: ' 封面不能为空', trigger: 'blur' }
          ],
          videoTime: [
            { required: true, message: '视频时长不能为空', trigger: 'blur' }
          ],
          seriesNum: [
            { required: true, message: ' 视频集数，如果需要上传多集，会用到不能为空', trigger: 'blur' }
          ],
          title: [
            { required: true, message: ' 标题不能为空', trigger: 'blur' }
          ],
          videoType: [
            { required: true, message: ' 0转载or1自制,默认自制不能为空', trigger: 'blur' }
          ],
          rePrintPower: [
            { required: true, message: ' 转载权限0不可转（默认）1可转不能为空', trigger: 'blur' }
          ],
          fromUrl: [
            { required: true, message: ' 转载自不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: ' 简介不能为空', trigger: 'blur' }
          ],
          uploadTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/common/video/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.uid = data.video.uid
                this.dataForm.pid = data.video.pid
                this.dataForm.level = data.video.level
                this.dataForm.status = data.video.status
                this.dataForm.url = data.video.url
                this.dataForm.cover = data.video.cover
                this.dataForm.videoTime = data.video.videoTime
                this.dataForm.seriesNum = data.video.seriesNum
                this.dataForm.title = data.video.title
                this.dataForm.videoType = data.video.videoType
                this.dataForm.rePrintPower = data.video.rePrintPower
                this.dataForm.fromUrl = data.video.fromUrl
                this.dataForm.remark = data.video.remark
                this.dataForm.uploadTime = data.video.uploadTime
                this.dataForm.updateTime = data.video.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/common/video/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'uid': this.dataForm.uid,
                'pid': this.dataForm.pid,
                'level': this.dataForm.level,
                'status': this.dataForm.status,
                'url': this.dataForm.url,
                'cover': this.dataForm.cover,
                'videoTime': this.dataForm.videoTime,
                'seriesNum': this.dataForm.seriesNum,
                'title': this.dataForm.title,
                'videoType': this.dataForm.videoType,
                'rePrintPower': this.dataForm.rePrintPower,
                'fromUrl': this.dataForm.fromUrl,
                'remark': this.dataForm.remark,
                'uploadTime': this.dataForm.uploadTime,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
